package ac.beiwo.baas.admin.controller;

import ac.beiwo.baas.admin.entity.Clazz;
import ac.beiwo.baas.admin.entity.ClazzAcl;
import ac.beiwo.baas.admin.service.ClazzService;
import ac.beiwo.baas.common.entity.SimpleError;
import ac.beiwo.baas.common.entity.SimpleResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static ac.beiwo.baas.common.entity.SimpleResult.success;

/**
 * Created by Staryet on 15/6/15.
 */
@RestController
@RequestMapping(value = "/admin/clazz")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @RequestMapping(value = "/", method = RequestMethod.POST)
    public SimpleResult insert(@RequestBody Clazz clazz) throws SimpleError {
        clazzService.insert(clazz);
        return success();
    }

    @RequestMapping(value = "/{name}", method = RequestMethod.DELETE)
    @ResponseBody
    public SimpleResult delete(@PathVariable String name) throws SimpleError {
        clazzService.delete(name);
        return success();
    }

    @RequestMapping(value = "/{name}", method = RequestMethod.GET)
    @ResponseBody
    public Clazz get(@PathVariable String name) throws SimpleError {
        return clazzService.get(name);
    }

    @RequestMapping(value = "/", method = RequestMethod.GET)
    @ResponseBody
    public List<Clazz> list() {
        return clazzService.list();
    }

    @RequestMapping(value = "/{name}/acl", method = RequestMethod.POST)
    @ResponseBody
    public SimpleResult setAcl(@PathVariable String name, @RequestBody ClazzAcl acl) throws SimpleError {
        clazzService.setAcl(name, acl);
        return success();
    }

}
